package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsGoodsAttributeService;
import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGoodsSpecificationService;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {

    @Autowired
    DtsGoodsMapper goodsMapper;
    @Autowired
    DtsGoodsProductService productService;
    @Autowired
    DtsGoodsAttributeService attributeService;
    @Autowired
    DtsGoodsSpecificationService specificationService;

    @Autowired
    DtsGoodsAttributeMapper attributeMapper;
    @Autowired
    DtsCategoryMapper categoryMapper;
    @Autowired
    DtsGoodsProductMapper productMapper;
    @Autowired
    DtsGoodsSpecificationMapper specificationMapper;

    /**
     * 查询总记录数
     * 是否被逻辑删除
     * @return
     */
    @Override
    public long getTotal() {
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getDeleted,false);
        Integer count = goodsMapper.selectCount(wrapper);
        return count;
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param goodsSn
     * @param name
     * @return
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String goodsSn, String name, List<Integer> brandIds) {
        IPage<DtsGoods> iPage = new Page<>(page,limit);

        QueryWrapper<DtsGoods> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("`name`",name);
        }
        if (!StringUtils.isEmpty(goodsSn)){
            wrapper.eq("`goods_sn`",goodsSn);
        }
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        //查找商品id
        if (brandIds != null && brandIds.size() > 0){
            wrapper.in("brand_id",brandIds);
        }

        goodsMapper.selectPage(iPage,wrapper);

        wrapper.eq("deleted",false);

        //封装结果集
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return data;
    }

    @Override
    public void insert(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();

        //商品编号
        LambdaQueryWrapper<DtsGoods> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DtsGoods::getGoodsSn,goods.getGoodsSn());
        Integer count1 = goodsMapper.selectCount(wrapper1);
        if (count1> 0){
            throw new RuntimeException("商品编号已存在");
        }
        //商品名称
        LambdaQueryWrapper<DtsGoods> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(DtsGoods::getName,goods.getName());
        Integer count2 = goodsMapper.selectCount(wrapper2);
        if (count2 > 0){
            throw new RuntimeException("商品名称已存在");
        }

        //新增
        goods.setAddTime(new Date());
        goods.setDeleted(false);
        goods.setUpdateTime(new Date());
        goodsMapper.insert(goods);

        //库存
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            productService.insert(product);
        }
        //属性
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attributeService.insert(attribute);
        }
        //规格
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specificationService.insert(specification);
        }

    }

    @Override
    public void delete(DtsGoods goods) {
        goodsMapper.deleteById(goods);
    }

    @Override
    public Map<String, Object> detail(Integer id) {
        //商品详情
        DtsGoods goods = goodsMapper.selectById(id);

        //商品属性
        LambdaQueryWrapper<DtsGoodsAttribute> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DtsGoodsAttribute::getGoodsId,id);
        wrapper1.eq(DtsGoodsAttribute::getDeleted,false);
        List<DtsGoodsAttribute> attributes = attributeMapper.selectList(wrapper1);

        //商品规格
        LambdaQueryWrapper<DtsGoodsSpecification> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(DtsGoodsSpecification::getGoodsId,id);
        wrapper2.eq(DtsGoodsSpecification::getDeleted,false);
        List<DtsGoodsSpecification> specifications = specificationMapper.selectList(wrapper2);

        //商品库存
        LambdaQueryWrapper<DtsGoodsProduct> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(DtsGoodsProduct::getGoodsId,id);
        wrapper3.eq(DtsGoodsProduct::getDeleted,false);
        List<DtsGoodsProduct> products = productMapper.selectList(wrapper3);

        //商品分类
        //先查二级分类
        Integer categoryId = goods.getCategoryId();

        DtsCategory category = categoryMapper.selectById(categoryId);
        //根据二级分类id查询一级分类
        Integer pid = category.getPid();

        //封装
        Map<String,Object> data = new HashMap<>();
        data.put("attributes",attributes);
        data.put("specifications",specifications);
        data.put("products",products);
        data.put("goods",goods);
        //
        data.put("categoryId", Arrays.asList(pid,categoryId));

        return data;
    }

    @Override
    public void update(GoodsAllinone goodsAllinone) {
        //获得商品属性
        DtsGoods goods = goodsAllinone.getGoods();
        //更新
        goods.setUpdateTime(new Date());
        goodsMapper.updateById(goods);

        //删除库存
        productService.deleteById(goods.getId());
        //删除属性
        attributeService.deleteById(goods.getId());
        //删除规格
        specificationService.deleteById(goods.getId());
        //新增库存
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            productService.insert(product);
        }
        //新增属性
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attributeService.insert(attribute);
        }
        //新增规格
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specificationService.insert(specification);
        }

    }
}
